﻿module Sustainalytics.Insights {

    import Common = Sustainalytics.Common;

    export class InsightsService implements IInsightsService {
        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private notificationFactory: Common.INotificationFactory;
        private staticObjects: Common.IStaticObjects;
        private $location: ng.ILocationService;

        //Ctor.
        constructor($q: ng.IQService, $http: ng.IHttpService, notificationFactory: Common.INotificationFactory, staticObjects: Common.IStaticObjects, $location: ng.ILocationService) {
            this.$q = $q;
            this.$http = $http;
            this.notificationFactory = notificationFactory;
            this.$location = $location;
        }

        private insightsUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/insightsserv/api/v1/insights/";
        }

        private filesUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/insightsserv/api/v1/files/";
        }

        // show error message for a given error code
        private showError(err) {
            var ctx = this;
            if (err && err['code'])
                ctx.notificationFactory.error('', ctx.staticObjects.getErrorMesageByStatusCode(err, err['code']));
        }

        private showMessage(msg) {
            var ctx = this;
            ctx.notificationFactory.success(msg);
        }

        public getInsightsList(skip: number, take: number, orderBy: string[], orderAscending: boolean) {
            var ctx = this;
            var def = this.$q.defer();

            var opt = new Common.ListOptions();
            opt.skip = skip;
            opt.take = take;
            opt.includeAllFields = true;
            opt.orderBy = orderBy;
            opt.orderAscending = orderAscending;

            var flt = new InsightFilterDto();
            flt.status = 1; //0 - Draft, 1 - Published, 3 - UnPublished
            flt.noLongerThan = moment().subtract('months', 3).format();

            
            var query =
                {
                    options: opt,
                    filter: flt
                };

            var url = '/insightsserv/api/v1/insights?' + $.param(query);
            var promise = this.$http.get(url);
            promise
                .success(function (data) {
                    def.resolve(data);
                })
                .error(function (err) {
                    ctx.showError(err);
                    def.reject();
                })

            return def.promise;
        }

        public getInsight(id: string): ng.IPromise<InsightDto> {

            var ctx = this;
            var def = this.$q.defer();
            var promise = this.$http.get(ctx.insightsUrl() + id, { params: { includeAllFields: true } });
            promise
                .success(data => { def.resolve(data); })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

    }

    // register service with News Angular module
    angular.module('Sustainalytics.Insights').factory('InsightsService', function ($q: ng.IQService, $http: ng.IHttpService, notificationFactory: Common.INotificationFactory, staticObjects: Common.IStaticObjects, $location: ng.ILocationService) {
        var srv = new InsightsService($q, $http, notificationFactory, staticObjects, $location);
        return srv;
    });
}